/**
 * 首页接口
 */

import * as api from "../services/home";

export default {
  namespace: "home",
  state: {
    userInfo: {},
    userUnRead: [],
    participles: [],
    appinfo: {},
  },

  effects: {
    // 获取当前用户的未读消息数
    *getUnRead({ payload }, { call }) {
      const res = yield call(api.getUnRead, payload);
      // totalCount
      // if (res && res.errCode === 0) {
      //   // yield put({ type: "save/userUnRead", payload: res.data });
      //   return res.data;
      // } else {
      //   return 0;
      // }
      return res;
    },

    *getHotSearch({ payload }, { call }) {
      const res = yield call(api.getHotSearch, payload);
      if (res && res.errCode === 0) {
        return res.data;
      } else {
        return null;
      }
    },

    *getRecommend({ payload }, { call }) {
      const res = yield call(api.getRecommend, payload);
      if (res && res.errCode === 0) {
        return res.data;
      } else {
        return null;
      }
    },
    *searchNews({ payload }, { call }) {
      const res = yield call(api.searchNews, payload);
      if (res && res.errCode === 0) {
        return res;
      } else {
        return null;
      }
    },
    *searchService({ payload }, { call }) {
      const res = yield call(api.searchService, payload);
      if (res && res.errCode === 0) {
        return res;
      } else {
        return null;
      }
    },
    *getParticiple({ payload }, { call, put }) {
      const res = yield call(api.getParticiple, payload);
      if (res && res.errCode === 0) {
        yield put({ type: "save/participle", payload: res.data });
        return true;
      } else {
        return null;
      }
    },
    *getToken({ payload }, { call, put }) {
      const res = yield call(api.getToken, payload);
      if (res && res.errCode === 0) {
        yield put({ type: "save/appinfo", payload: res.data });
        return res.data;
      } else {
        return null;
      }
    },
    *uploadAudio({ payload }, { call }) {
      const { form } = payload;
      console.log(form);
      const res = yield call(api.uploadAudio, form);
      return res;
    },
  },
  reducers: {
    "save/userUnRead": (state, { payload }) => ({
      ...state,
      userUnRead: payload,
    }),
    "save/appinfo": (state, { payload }) => ({
      ...state,
      appinfo: payload,
    }),
    "save/salt": (state, { payload }) => ({
      ...state,
      salt: payload,
    }),
    "save/participle": (state, { payload }) => ({
      ...state,
      participles: payload,
    }),
  },
};
